<template>
	<view><!-- <view>{{babyinfo}}</view> -->
		<view class="cu-form-group margin-top">
			<view class="title">宝宝小名：</view>
			<input type="text" v-model="babyconent.babynike" placeholder="输入宝宝名称" />
		</view>
		<radio-group style="border-top: 1upx solid #eee;" name="bb_id" class="block" @change="RadioChange">
			<view class="cu-form-group">
				<view class="title">宝宝性别：</view>
				<label>
					<radio value="0" :checked="babyconent.sex==0?true:false"/><text>女宝</text>
				</label>
				<label>
					<radio value="1" :checked="babyconent.sex==1?true:false"/><text>男宝</text>
				</label>
				<label>
					<radio value="2" :checked="babyconent.sex==2?true:false" /><text>保密</text>
				</label>
			</view>
		</radio-group>

		<view class="cu-form-group" style="border-top: 1upx solid #eee;">
			<view class="title">宝宝生日：</view>
			<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="DateChange">
				<view class="picker">
					{{date}}
				</view>
			</picker>
		</view>
		<input v-if="id==0" type="number" :value="babyconent.user_id" name='user_id' v-show="false" />
		<input v-else type="number" :value="id" name='bb_id' v-show="false" />
		
		<view class="cu-form-group" v-show="false">
			<view class="title">默认状态：</view>
			<switch @change="SwitchA" :class="switchA?'checked':''" :checked="switchA?true:false"></switch>
			</switch>
		</view>

		<view class="padding flex flex-direction margin-top">
			<button v-if="id==0" class="cu-btn bg-red margin-tb-sm lg" @click="addbaby">添加</button>
			<button v-else class="cu-btn bg-red margin-tb-sm lg" @click="modbaby">修改</button>
			<button class="cu-btn margin-tb-sm bg-gradual-green lg" @click="back">返回</button>
		</view>
		<view>{{babyconent}}</view>
		<view style="margin: 20rpx;">注意：有且只能一个宝宝作为默认状态/当有一个宝宝当时候该选项设置禁用。</view>
	</view>
</template>

<script>
	import url_config from '@/common/config.js'
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				id: 0,
				babyconent: {
					user_id: 0,
					babynike: '',
					sex: 2,
					birthday: '2022-02-03',
					status: 0
				},
				babyinfo: [],
				switchA: false,
				date: currentDate,

			}
		},
		methods: {
			//获取日期
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			SwitchA(e) {
				this.switchA = e.detail.value
			},
			RadioChange(e) {
				this.babyconent.sex = e.detail.value
			},
			DateChange(e) {
				this.date = e.detail.value
				this.babyconent.birthday = e.detail.value
			},
			addbaby() {
				//添加宝宝
				uni.request({
					url: url_config.api + 'addBaby2',
					data: this.babyconent,
					method: 'POST',
					success: (res) => {

						//获取添加的宝宝数据，并判断其中ischoose=1的情况设置缓存，作为初始值
						if (res.data.code == 200) {
							if (res.data.data.ischoose == 1) {
								try {
									uni.setStorageSync('bb_id', res.data.data.id)
								} catch (e) {
									//TODO handle the exception
								}
							}
						}

						uni.navigateBack({
							delta: 1
						})
					},
					fail: () => {
						uni.showToast({
							title: '添加失败',
							duration: 2000
						})
					}
				})
			},
			modbaby(){
				//修改宝宝信息
				uni.request({
					url:url_config.api + 'editBaby',
					method:'PUT',
					data:this.babyconent,
					success: (res) => {
						if(res.data.code==200){
							uni.showToast({
								title:'修改成功',
								duration:1000
							})
							//console.log(res.data.data)
						}else{
							console.log('err')
						}
					},
					fail() {
						console.log('失败')
					}
				})
			},
			back(){
				uni.navigateBack({
					delta:1
				})
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}

		},
		onLoad: function(option) {
			//console.log(option)
			if (option.bid) {
				this.id = option.bid
				//修改
				this.babyconent['id'] = option.bid
				//修改时删除user_id
				delete this.babyconent.user_id
				//对象中添加元素this.$set(this.babyconent,'id',option.bid)
				uni.request({
					url: url_config.api + 'getBabyInfo2/' + option.bid,
					success: (res) => {
						this.babyinfo = res.data
						this.babyconent.babynike = res.data.data.babynike
						this.date = res.data.data.birthday
						//0女1男2保密
						let sex = res.data.data.sex
						if(sex =='女宝'){
							this.babyconent.sex = 0;
						}else if(sex =='男宝'){
							this.babyconent.sex = 1;
						}else{
							this.babyconent.sex = 2;
						}
						
						//console.log(res.data.data)
					}
				})
			} else {
				//添加
				this.babyconent.user_id = option.uid
			}

		}
	}
</script>

<style>

</style>
